package com.laex;

import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class LaexianPOE extends MIDlet implements CommandListener,
		ItemCommandListener {

	private DisplayManager manager;

	private MenuManager menuScr;
	private CategoryManager catScr;
	private ExpenseManager expScr;
	private ExpenseReportView expView;
	private CategoryListView catListView;
	private QuestionAlert confirmationView;
	private PieChartManager pieChartManager;

	private TextBox displayDone;
	private Alert expenseTotal;

	private Command back;
	private Command exit;

	/**
	 * Creates several screens and navigates between them.
	 * 
	 * @throws IOException
	 */
	public LaexianPOE() throws IOException {
		this.menuScr = new MenuManager(this);
		this.manager = new DisplayManager(Display.getDisplay(this), menuScr);

		this.back = new Command("Back", Command.BACK, 1);
		this.exit = new Command("Exit", Command.EXIT, 1);

		this.catScr = new CategoryManager(back);
		this.catScr.setCommandListener(this);

		this.expScr = new ExpenseManager(back);
		this.expScr.setCommandListener(this);

		this.catListView = new CategoryListView(back);
		this.catListView.setCommandListener(this);

		this.expView = new ExpenseReportView(back);
		this.expView.setCommandListener(this);

		this.displayDone = new TextBox("Status", "Done! Please restart the app.", 100, TextField.UNEDITABLE);
		this.displayDone.addCommand(back);
		this.displayDone.setCommandListener(this);

		this.expenseTotal = new Alert("Total", "", null, AlertType.INFO);
		this.expenseTotal.addCommand(back);
		this.expenseTotal.setCommandListener(this);

		this.confirmationView = new QuestionAlert(this, QuestionAlert.NONE);
		
		
		this.pieChartManager = new PieChartManager();
		this.pieChartManager.addCommand(back);
		this.pieChartManager.setCommandListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		this.manager.next(this.menuScr);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.microedition.lcdui.CommandListener#commandAction(javax.microedition
	 * .lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command command, Displayable displayable) {
		if (command == catScr.getAddCommand()) {
			catScr.addCategory();
		} else if (command == expScr.getSelectCatCommand()) {
			catListView.loadList();
			manager.next(catListView);
		} else if (command == expScr.getAddCommand()) {
			expScr.addExpense(catListView.getSelectedCategory());
		} else if (command == confirmationView.getYesCMD()) {
			if (confirmationView.getCurrent().equals(QuestionAlert.CAT)) {
				RecordUtil.deleteAllCategories();
			} else if (confirmationView.getCurrent().equals(QuestionAlert.EXP)) {
				RecordUtil.deleteAllExpenses();
			}
			manager.next(displayDone);
		} else if (command == confirmationView.getNoCMD() || command == back) {
			manager.back();
		} else if (command == exit) {
			this.notifyDestroyed();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {
	}

	public void commandAction(Command command, Item item) {
		if (command == menuScr.getCategoryEntryCMD()) {
			manager.next(catScr);
		} else if (command == menuScr.getExpenseEntryCMD()) {
			manager.next(expScr);
		} else if (command == menuScr.getReportViewCMD()) {
			expView.loadReport();
			manager.next(expView);
		} else if (command == menuScr.getDeleteAllExpensesCMD()) {
			confirmationView.setString(QuestionAlert.EXP);
			manager.next(confirmationView);
		} else if (command == menuScr.getDeleteAllCategoiesCMD()) {
			confirmationView.setString(QuestionAlert.CAT);
			manager.next(confirmationView);
		} else if (command == menuScr.getExpenseSummaryCMD()) {
			expenseTotal.setString(RecordUtil.getExpenseTotal());
			manager.next(expenseTotal);
		} else if (command == menuScr.getPierChartCMD()) {
			pieChartManager.drawPieChart(RecordUtil.getPieChartData(),RecordUtil.getCategories());
			manager.next(pieChartManager);
		}
	}

}
